package com.hotel.filter; /**
 * Created by Enzo Cotter on 2021/5/14.
 */

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

@WebFilter(
        filterName = "SessionFilter",
        urlPatterns = {
                "/jsp/addAddress.jsp",
                "/jsp/orderbocuiHotel.jsp",
                "/jsp/orderjianguoHotel.jsp",
                "/jsp/orderjinglunHotel.jsp",
                "/jsp/ordernanyuanHotel.jsp",
                "/jsp/wodecahngyongdizhi.jsp",
                "/jsp/wodedingdanxinxi.jsp",
                "/jsp/wodefapiaoguanli.jsp",
                "/jsp/wodejiabinhui.jsp",
                "/jsp/wodeshoucangjiudian.jsp",
                "/jsp/wodexinxi.jsp",
                "/jsp/xiugaixinxi"
        },
        dispatcherTypes = {
                DispatcherType.REQUEST,
                DispatcherType.FORWARD
        }
)
public class SessionFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        req.getSession().setAttribute("pageUpURI",req.getRequestURI());
        if (req.getSession().getAttribute("curUser") == null) {
            req.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
        }
        chain.doFilter(request, response);
    }
}
